# coding:utf-8
__author__ = 'chenghao'

import urllib2, json
from bottle import Bottle

current_ip_app = Bottle()


@current_ip_app.get("/")
def index():
	try:
		req = urllib2.Request("http://ip.taobao.com/service/getIpInfo2.php?ip=myip")
		res_data = urllib2.urlopen(req, timeout=30)
		res = res_data.read()

		m = json.loads(res)
		if m.get("code") == 0:
			d = m.get("data")
			return {"code": 0, "ip": d.get("ip"), "area": d.get("area"), "area_id": d.get("area_id"),
					"region": d.get("region"), "region_id": d.get("region_id"), "city": d.get("city"),
					"city_id": d.get("city_id"), "isp": d.get("isp")}
		else:
			return {"code": -20, "msg": "查询数据为空"}
	except Exception, e:
		# print e
		return {"code": -10, "msg": "系统异常"}
